//
//  SFADatePicker.m
//  AlarmClock
//
//  Created by Fedor Skrynnikov on 10/15/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "SFADatePicker.h"
#import <QuartzCore/QuartzCore.h>


#define NUMBER_OF_DAYS  1000
#define NUMBER_OF_HOURS 12
#define NUMBER_OF_MIN   60
#define NUMBER_OF_AMPM  2

#define DAY_ROW_WIDTH       130.0
#define HOURS_ROW_WIDTH     40.0
#define MIN_ROW_WIDTH       40.0
#define AMPM_ROW_WIDTH      40.0

#define DATES_KEY   @"dates"
#define HOURS_KEY   @"hours"
#define MINS_KEY    @"minutes"
#define AMPM_KEY    @"ampms"

@implementation SFADatePicker

@synthesize today = _today;
@synthesize selectedDate = _selectedDate;

- (id)init{
    self = [super init];
    if (self) {
        [self initialization];

    }
    return self;
}

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialization];
    }
    return self;
}

-(void) initialization{
    [self setDelegate:self];
    [self setDataSource:self];
    [self setShowsSelectionIndicator:YES];
    self.today = [[NSDate alloc] init];
    
    model = [[NSDictionary alloc]initWithObjectsAndKeys:[[NSMutableArray alloc] init], DATES_KEY,
                                                        [[NSMutableArray alloc] init], HOURS_KEY,
                                                        [[NSMutableArray alloc] init], MINS_KEY,
                                                        [[NSMutableArray alloc] init], AMPM_KEY, nil];
    NSDateFormatter* formater = [[NSDateFormatter alloc]init];
    [formater setDateFormat:@"EEE, dd MMM ''yy"];
    
    for (int i = 0; i < NUMBER_OF_DAYS; i++) {
        [[model objectForKey:DATES_KEY] addObject:[formater stringFromDate:[NSDate dateWithTimeInterval:(60.0f*60.0f*24.0f*i) sinceDate:self.today]]];
    }
    [formater release];
    
    for (int i = 1; i <= NUMBER_OF_HOURS; i++) {
        [[model objectForKey:HOURS_KEY] addObject:[NSString stringWithFormat:@"%d",i]];
    }
    for (int i = 0; i < NUMBER_OF_MIN; i++) {
        [[model objectForKey:MINS_KEY] addObject:[NSString stringWithFormat:@"%d",i]];
    }
    for (int i = 0; i < NUMBER_OF_AMPM; i++) {
        [[model objectForKey:AMPM_KEY] addObject:i == 0 ? @"AM" : @"PM"];
    }
}

- (void) dealloc{
    [_today release];
    [super dealloc];
}

- (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 4;  
}

- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    static int numberInComponent[] = {NUMBER_OF_DAYS, NUMBER_OF_HOURS, NUMBER_OF_MIN, NUMBER_OF_AMPM};
    return numberInComponent[component];
}

- (UIView*) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* label = [[[UILabel alloc]init] autorelease];
    [label setBackgroundColor:[UIColor clearColor]];
    [label setTextColor:[UIColor blackColor]];
    [label setFont:[UIFont boldSystemFontOfSize:16]];
    
    NSArray* keysForComponent = [NSArray arrayWithObjects:DATES_KEY, HOURS_KEY, MINS_KEY, AMPM_KEY, nil];
    
    [label setText:[[model objectForKey:[keysForComponent objectAtIndex:component]] objectAtIndex:row] ];

    [label setTextAlignment:UITextAlignmentCenter];
    return label;
}

- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    static int widthForComponent[] = {DAY_ROW_WIDTH, HOURS_ROW_WIDTH, MIN_ROW_WIDTH, AMPM_ROW_WIDTH};
    return widthForComponent[component];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSDateFormatter* formater = [[NSDateFormatter alloc]init];
    NSLocale* locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [formater setLocale:locale];
    [formater setDateFormat:@"EEE, d MMM ''yy hh:mm a"];
    NSUInteger dateIndex = [pickerView selectedRowInComponent:DATE_INDEX];
    NSUInteger hourIndex = [pickerView selectedRowInComponent:HOUR_INDEX];
    NSUInteger minIndex = [pickerView selectedRowInComponent:MINUTE_INDEX];
    NSUInteger ampmIndex = [pickerView selectedRowInComponent:AMPM_INDEX];
    NSMutableString* date = [[NSMutableString alloc]initWithString:[[model objectForKey:DATES_KEY] objectAtIndex:dateIndex]];
    [date appendString:@" "];
    [date appendString:[[model objectForKey:HOURS_KEY] objectAtIndex:hourIndex]];
    [date appendString:@":"];
    [date appendString:[[model objectForKey:MINS_KEY] objectAtIndex:minIndex]];
    [date appendString:@" "];
    [date appendString:[[model objectForKey:AMPM_KEY] objectAtIndex:ampmIndex]];
    self.selectedDate = [[formater dateFromString: date] retain];
    [date release];
    [locale release];
    [formater release];
}

@end
